package com.example.bumenguanli.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
//和过滤器功能重了
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/api/**", "/login", "/register", "/login.html",
                        "/register.html", "/index.html", "/workers", "/workers/**","/css/**", "/js/**", "/images/**").permitAll()
                .anyRequest().authenticated()
                .and()
                // 移除formLogin配置，使用自定义API登录
                .httpBasic().disable() // 禁用基本认证
                .formLogin().disable() // ❗️关键：禁用表单登录
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login.html")
                .permitAll();
    }
}